﻿using System.Linq;
using GameCreator.Camera;
using GameCreator.Core;
using Gp.GCX;
using Gp.Scripts.Core.Command;
using Gp.Scripts.GameLogic;
using QFramework;
using Sirenix.OdinInspector;
using UnityEditor;


namespace Gp.Scripts.Core {
    public abstract class BasePuzzle : BaseDynamicObject, IController {
        [LabelText("解谜机位")]
        public CameraMotor cameraMotor;

        [LabelText("解谜名")]
        public string puzzleName;

        [LabelText("解谜触发器")]
        public Trigger trigger;

        public bool IsInteractable { get; private set; }

        public bool TriggerFlag {
            get;
            private set;
        }


        protected override void OnInit() {
            TriggerFlag = Model.TriggeredFlag(SaveKey);
            if (TriggerFlag) {
                MarkPuzzleWorkOut();
            }
        }


        public virtual void EnablePuzzle() {
            // if(TriggerFlag) return;
            // IsInteractable = true;
            //
            //
            // if (cameraMotor == null) return;
            // this.SendCommand(new ChangeCameraMotorCommand(cameraMotor));
            // trigger.gameObject.SetActive(false);
        }

        
        public virtual void DisablePuzzle() {
            // if (!IsInteractable) return;
            // IsInteractable = false;
            //
            //
            // if (cameraMotor == null) return;
            // this.SendCommand<ChangeCameraMotorCommand>();
            //
            //
            // if(trigger == null) return;
            // trigger.gameObject.SetActive(true);
            // var igniter = trigger.igniters.Select(x => x.Value).FirstOrDefault(x => x is InteractionIgniter) as InteractionIgniter;
            // if (igniter != null) {
            //     igniter.ShowDrawer();
            // }
        }
        

        public virtual void TryPuzzle() { }

        public virtual void MarkPuzzleWorkOut() {
            TriggerFlag = true;
            DisablePuzzle();
        }
        
        
        
        
        
#if UNITY_EDITOR
        [Button("查看解谜视角", ButtonSizes.Gigantic)]
        public void SceneCameraToBattleView() {
            if (!cameraMotor) return;
            SceneView sceneView = SceneView.lastActiveSceneView;
            sceneView.AlignViewToObject(cameraMotor.transform);
        }
#endif
        public IArchitecture GetArchitecture() {
            return SceneScriptArchitecture.Interface;
        }
    }
}